import { AppContext } from "../app.context";
import { UserInfo } from "../mode/user";
import { BaseServices } from "./base.services";
import {
  APIMethod,
  APIUrl,
} from "./parameters"

export interface LoginResp {
  code: number
  data: LoginInfo
  message: string
}

interface UserResp {
  code: number
  data: UserInfo
  message: string
}

export interface LoginInfo {
  token: string;
  info?: UserInfo;
  openid?: string;
}

export class LoginService extends BaseServices {
  private static _instance: LoginService;
  public static get instance(): LoginService {
    if (this._instance == null) {
      this._instance = new LoginService();
    }
    return this._instance;
  }
  constructor() { super() }

  /**
   * 账号密码形式登录
   * @param account 
   * @param password 
   * @returns 
   */
  login(account: string, password: string) {
    const url = APIUrl.Login + APIMethod.Login
    const data = { account, password }
    return new Promise<LoginResp>((resolve, reject) => {
      this.request<LoginResp>(url, data).then(res => {
        console.log(res);
        AppContext.instance.setToken(res.data.token);

        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }

  /**
   * 微信快速登录
   * @param code 
   * @returns 
   */
  loginWX(code: string) {
    const url = APIUrl.Login + APIMethod.Code2Session
    const data = { code }
    return new Promise<LoginResp>((resolve, reject) => {
      this.request<LoginResp>(url, data).then(res => {
        console.log(res);
        AppContext.instance.setToken(res.data.token);
        if (res.data.info) {

          AppContext.instance.user.setData(res.data.info);
          try {
            wx.setStorageSync('openid', res.data.info.openid)
          } catch (e) { }
        } else {
          try {
            wx.setStorageSync('openid', res.data.openid)
          } catch (e) { }
          // wx.reLaunch({
          //   url: '/pages/mine/login/login'
          // })
        }
        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }

  /**
   * 获取登录用户信息
   * @returns 
   */
  getUserInfo() {
    const url = APIUrl.Login + APIMethod.Info
    const data = {}
    return new Promise<LoginResp>((resolve, reject) => {
      this.request<LoginResp>(url, data).then(res => {
        console.log(res);

        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }

  /**
   * 解密用户信息
   * @returns 
   */
  decryptionWX(encryptedData: string, iv: string) {
    const openid = AppContext.instance.user.openid;
    const url = APIUrl.Login + APIMethod.DecryptionWX
    const data = { openid, encryptedData, iv }
    return new Promise<UserResp>((resolve, reject) => {
      this.request<UserResp>(url, data).then(res => {
        console.log(res);
        AppContext.instance.user.setData(res.data);
        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }

  /** 退出登录 */
  logOut() {
    const uid = AppContext.instance.user.data.uid;
    const url = APIUrl.Login + APIMethod.LogOut
    const data = { uid }
    return new Promise<UserResp>((resolve, reject) => {
      this.request<UserResp>(url, data).then(res => {
        console.log(res);
        AppContext.instance.user.clear();
        resolve(res)
      }, err => {
        reject(err);
      })
    });
  }
}